<?php



class C_Test extends Controller {

	var $template_mod;					
	
	function C_Ticket($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Test') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'test';
		}
		$this->template_mod = $template_mod;
		
		if (!isset($_SESSION['clearhealth']['filters'][get_class($this)])) {
			$_SESSION['clearhealth']['filters'][get_class($this)] = array();
		}
		$this->filters = $_SESSION['clearhealth']['filters'][get_class($this)];
		$this->assign("filters",$this->filters);

		$this->view->path = 'test';
	}
	
	public function actionListItemAjax() {
		$testId = $_GET['test_id'];
		$testServ = Celini::newORDO('TestService');
		$result = $testServ->listTestItem($testId);		
		return $this->buildItemHTML($result, $testId);
	}
	
	private function buildItemHTML($data, $testId) {
		$mod = sizeof($data) % 3;
		$div = floor(sizeof($data) / 3);
		
		$row = ($mod > 0)?$div+1:$div; 
		$firstCol = $row;
		$secondCol = ($mod == 2)?$row:$row-1;
		$thirdCol = ($mod == 0)?$row:$row-1; 
		
		$html = "";
		$html .= "<input id=\"test_id\" type=\"hidden\" value=\"$testId\">";
		$html .= "<table id=\"test_item_data\" class=\"details\" cellspacing=\"0\" cellpaddding=\"0\">";		
		$count = 0;
		$item = 0;
		for ($i = 0; $i < $row; $i++) {						
			$id = $count;			
			$html .= "<tr>";
			$html .= $this->addTD($data, $id, $item);			
			$item++;
			
			$id = $id + $firstCol;
			$html .= $this->addTD($data, $id, $item);			
			$item++;
			
			$id = $id + $secondCol;
			$html .= $this->addTD($data, $id, $item);
			$item++;
			$html .= "</tr>";
			$count++;
		}		
		$html .= "</table>";
		
		return $html;
	}
	
	private function addTD($data, $id, $item) {		
		$cond = ($item<sizeof($data))?TRUE:FALSE;
		if ($cond) {
			$serviceId = $data[$id]['service_id'];
			$name = $data[$id]['name'];			
			$html = "<td><input class=\"test_item_popup\" id=\"$serviceId\" type=\"checkbox\" value=\"$serviceId\"></td>";
			$html .= "<td class=\"label\"><label for=\"$serviceId\">$name</label></td>";						
			return $html;
		} else {
			return "<td></td>";
		}
		
	
	}
}
?>
